/*******************************************************************************
Module  : Implementation-Datei fuer das Modul "SPI_MCP2510"
Extends : SPI
Version : 1.0
Date    : 24.06.2007
Author  : Guenther Pammer

Compilerswitches: keine
*******************************************************************************/
#include "spi_mcp2510.h"

/**
* SPI_MCP2510_reset
* Reset des MCP2510 CAN-Contrllers ueber SPI-Schnittstelle
*/
void SPI_MCP2510_reset ( void ) {
  SS_ENABLE;
  SPI_write_char ( SPI_RESET );
  SS_DISABLE;
}

/**
* SPI_MCP2510_sync
* Syncronisation mit dem MCP2510 CAN-Controllers
*/
void SPI_MCP2510_sync (void) {
  int i;

  SS_ENABLE;
  SPI_write_char ( 0xFF );             // Adresse zuerst
  SPI_write_char ( SPI_READ | 0x0F );

  for ( i=0; i<14; i++) {              // 15x 0xFF
     SPI_write_char ( 0xFF );
  }
  SS_DISABLE;
}

/**
* SPI_MCP2510_reg_read
* Register des CAN-Controllers lesen
* \param addr ... Adresse des Registers
* \return Inhalt des angeforderten Registers
*/
unsigned char SPI_MCP2510_reg_read ( unsigned char addr ) {
  unsigned char byte;

  SS_ENABLE;
  SPI_write_char ( SPI_READ );
  SPI_write_char ( addr );
  SPI_read_char  ( &byte );
  SS_DISABLE;
  return byte;
}

/**
* SPI_MCP2510_reg_write
* Register des CAN-Controllers schreiben
* \param addr ... Adresse des Registers
* \param data ... Daten die in das Register geschrieben werden
*/
void SPI_MCP2510_reg_write ( unsigned char addr, unsigned char byte ) {
  SS_ENABLE;
  SPI_write_char ( SPI_WRITE );
  SPI_write_char ( addr );
  SPI_write_char ( byte );
  SS_DISABLE;
}

/**
* SPI_MCP2510_reg_bit_modify
* Bitmanipulation in einem Register des CAN-Controllers
* \param addr ... Adresse des Registers
* \param mask ... Maske zur Bitmanipulation (Bit wird manipuliert)
* \param data ... Daten die in das Register geschrieben werden
*/
void SPI_MCP2510_reg_bit_modify (unsigned char addr, unsigned char mask,
                                 unsigned char byte) {
  SS_ENABLE;
  SPI_write_char (SPI_BIT_MODIFY);
  SPI_write_char ( addr );
  SPI_write_char ( mask ); // mask-Bit = 1 --> entspr. Registerbit wird manip.
  SPI_write_char ( byte );
  SS_DISABLE;
}
